﻿using MVVM.Models;
using MVVM.Models.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xceed.Wpf.Toolkit;

namespace MVVM.ViewModels
{
    class MainWindowViewModel : NotificationObject
    {
        private Restaurant restaurantInfo;
        public Restaurant RestaurantInfo
        {
            get { return restaurantInfo; }
            set
            {
                restaurantInfo = value;
                RaisePropertyChanged("RestaurrantInfo");
            }
        }

        private ObservableCollection<DishMenuViewModel> dishMenu;
        public ObservableCollection<DishMenuViewModel> DishMenu
        {
            get { return dishMenu; }
            set
            {
                dishMenu = value;
                RaisePropertyChanged("DishMenu");
            }
        }

        private int count;
        public int DishMenuSelectedCount
        {
            get { return count; }
            set
            {
                count = value;
                RaisePropertyChanged("DishMenuSelectedCount");
            }
        }

        public DelegateCommand DishMenuSelectedCommand { get; set; }
        public DelegateCommand PlaceOrderCommand { get; set; }
        public ActionCommand DeleteMenuItemCommand { get; set; }

        public ActionCommand EditRestaurantInfoCommand { get; set; }
        public ActionCommand EditDishMenuCommand { get; set; }

        public MainWindowViewModel()
        {
            LoadRestaurantInfo();
            LoadDishMenu();

            this.DishMenuSelectedCommand = new DelegateCommand(DishMenuSelectedCommandExecute);
            this.PlaceOrderCommand = new DelegateCommand(PlaceOrderCommandExecute);
            this.DeleteMenuItemCommand = new ActionCommand(DeleteMenuItemCommandExecute);

            this.EditRestaurantInfoCommand = new ActionCommand(EditRestaurantInfoCommandExecute);
            this.EditDishMenuCommand = new ActionCommand(EditDishMenuCommandExecute);
        }

        private void LoadRestaurantInfo()
        {
            RestaurantService restaurantSvr = new RestaurantService();
            this.RestaurantInfo = restaurantSvr.GetRestaurantInfo();
        }

        private void LoadDishMenu()
        {
            this.DishMenu = new ObservableCollection<DishMenuViewModel>();

            DishService dishSvr = new DishService();
            List<Dish> dishList = dishSvr.GetAllDishes();
            foreach (Dish item in dishList)
            {
                DishMenu.Add(new DishMenuViewModel()
                    {
                        Dish = item,
                        IsSelected = false
                    });
            }
        }

        private void DishMenuSelectedCommandExecute()
        {
            this.DishMenuSelectedCount = DishMenu.Count(c => c.IsSelected == true);
        }

        private void PlaceOrderCommandExecute()
        {
            var orderList = DishMenu.Where(c => c.IsSelected == true).Select(c => c.Dish);
            foreach (var item in orderList)
            {
                string name = item.Name;
            }
            OrderService orderSvr = new OrderService();
            orderSvr.SaveOrder(orderList.ToList());
            MessageBox.Show("定单设置成功！", "^_^", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information);
        }

        private void DeleteMenuItemCommandExecute(object parameter)
        {
            DishMenuViewModel menuItem = parameter as DishMenuViewModel;
            DishMenu.Remove(menuItem);
            MessageBox.Show("菜单项删除成功！");
        }

        private void EditRestaurantInfoCommandExecute(object parameter)
        {
            RestaurantService restaurantSvr = new RestaurantService();
            restaurantSvr.SetRestaurantInfo(this.RestaurantInfo);
            MessageBox.Show("信息已经保存，重启后生效！");
        }

        private void EditDishMenuCommandExecute(object parameter)
        {
            DishService dishSvr = new DishService();
            List<Dish> dishList = new List<Dish>();
            foreach (var item in this.DishMenu)
            {
                dishList.Add(item.Dish);
            }
            dishSvr.SetAllDishes(dishList);
            MessageBox.Show("信息已经保存，重启后生效！");
        }
    }
}
